package com.famid.flashcards;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.famid.flashcards.data.Card;
import com.famid.flashcards.data.CardsDAO;
import com.famid.flashcards.data.CardsDAOImpl;
import com.famid.flashcards.data.Card.KnownStatus;
import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class NewFlashCardActivity extends Activity {

	private static final String TAG = NewFlashCardActivity.class.getName();
	private EditText myInputText;
	private EditText myOutputText;
	private Context cxt;
	private CardsDAO cardsDao;
	private long stackId;
	private boolean edit = false;
	private long cardId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_flashcard);
		
		cxt = this;
		cardsDao = new CardsDAOImpl(cxt);
		cardsDao.open();
		
		Bundle bundle = this.getIntent().getExtras();
		stackId = bundle.getLong("stackId");
		edit = bundle.getBoolean("edit");

		myInputText = (EditText) findViewById(R.id.questionText);
		myOutputText = (EditText) findViewById(R.id.answerText);
		
		if(edit) {
			cardId = bundle.getLong("cardId");
			myInputText.setText(bundle.getString("Q"));
			myOutputText.setText(bundle.getString("A"));
		}
	}
	
	@Override
	protected void onResume() {
		cardsDao.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		cardsDao.close();
		super.onPause();
	}
	
	public void onAddCardClick(View v) {
		if("".equalsIgnoreCase(myInputText.getText().toString()) || "".equalsIgnoreCase(myOutputText.getText().toString()))
			Toast.makeText(cxt, "You need to enter both side of card!", Toast.LENGTH_SHORT).show();
		else {
			if(!edit) {
				Log.d(TAG, "Create new card Q:"+myInputText.getText().toString()+" "+myOutputText.getText().toString());
				Card card = new Card(0l, myInputText.getText().toString(), myOutputText.getText().toString(), KnownStatus.UNKNOWN, stackId);
				cardsDao.createCard(card);
				Log.d(TAG, "Create card created");
			}
			else {
				Log.d(TAG, "Edit card id: "+cardId);
				Card card = cardsDao.get(cardId);
				card.setQuestion(myInputText.getText().toString());
				card.setAnswer(myOutputText.getText().toString());
				cardsDao.update(card);
				Log.d(TAG, "Card changed");
			}
			finish();
		}
	}
	
	public void onCancleClick(View v) {
		Log.d(TAG, "Cancle activity");
		finish();
	}


}
